package cn.edu.ctbu.finaltest.service.AdministratorService.impl;


import cn.edu.ctbu.finaltest.domain.Administrator;
import cn.edu.ctbu.finaltest.exception.REnum;
import cn.edu.ctbu.finaltest.repository.AdministratorRepository;
import cn.edu.ctbu.finaltest.service.AdministratorService.AdministratorService;
import cn.edu.ctbu.finaltest.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AdministratorImplService implements AdministratorService {

    @Autowired
    private AdministratorRepository administratorRepository;

    /**
     * 查询所有管理员
     * @return 返回List
     */
    public List<Administrator> findAll(){
        return administratorRepository.findAll();
    }
    public R<List<Administrator>> findRAll(){
        R<List<Administrator>> RAll = new R<>();
        RAll.setData(administratorRepository.findAll());
        RAll.setCode(REnum.SUCCESS.getCode());
        RAll.setMsg(REnum.SUCCESS.getMsg());
        return RAll;
    }

    public R<List<Administrator>> findByRSex(String sex){
        List<Administrator> administrators = administratorRepository.findAll();
        List<Administrator> administratorsNew = new ArrayList<>();
        for (int i = 0;i < administrators.size();i++){
            if (sex.equals(administrators.get(i).getSex())){
                administratorsNew.add(administrators.get(i));
            }
        }
        R<List<Administrator>> RAll = new R<>();
        RAll.setData(administratorsNew);
        RAll.setCode(REnum.SUCCESS.getCode());
        RAll.setMsg(REnum.SUCCESS.getMsg());
        return RAll;
    }


    /**
     * 工号密码查询管理员
     * @param Ano
     * @param password
     * @return
     */
    public Administrator findByAnoAndPassword(String Ano,String password){
        List<Administrator> administrators = administratorRepository.findAll();
        Administrator administrator = new Administrator();
        for (int i = 0;i < administrators.size();i++){
            if (Ano.equals(administrators.get(i).getAno()) && password.equals(administrators.get(i).getPassword())){
                administrator = administrators.get(i);
                break;
            }
        }
        return administrator;
    }


    /**
     * 管理员信息的增加
     * @param administrator
     * @return 返回Administrator
     */
    public Administrator insert(Administrator administrator){
        administratorRepository.save(administrator);
        return administrator;
    }

    /**
     * 管理员信息的更新
     * @param administrator
     * @return
     */
    public Administrator update(Administrator administrator){
        administratorRepository.save(administrator);
        return administrator;
    }

    /**
     * 管理员信息通过id删除
     * @param id
     */
    public void deleteId(Integer id){
        administratorRepository.deleteById(id);
    }
}
